﻿@model Von.Web.Models.RoleRightViewModel
@{
    ViewBag.Title = "角色权限设定管理";
}

<h2>角色权限设定管理</h2>
<input id="RoleName" type="hidden" value="@Model.RoleName" />
<div class="row">
    <div class="col-md-2 col-sm-3">
        <div class="panel panel-info">
            <div class="panel-heading">
                <h3 class="panel-title">菜单权限</h3>
            </div>
            <div class="panel-body">
                <table id="MenuTree" title="系统菜单" class="easyui-tree"></table>
            </div>
        </div>
    </div>
    <div class="col-md-9">
        <div id="editor">
            @{System.Drawing.Color currentColor = System.Drawing.Color.FromArgb(224, 204, 255); }
            @foreach (var m in Model.AppRights)
            {
                currentColor = Von.Library.Functions.GetNextColor(currentColor, 5);
                var fntColor = Von.Library.Functions.GetFontColor(currentColor);
                <div class="panel">
                    <div class="panel-heading" style="background-color:@string.Format("#{0:X2}{1:X2}{2:X2}", currentColor.R, currentColor.G, currentColor.B)">
                        <input type="hidden" value="@m.AppID" />
                        <a style="float:right" class="close" onclick="selectAll(this)"><span class="glyphicon glyphicon-ok-circle"></span></a>
                        <a style="float:right" class="close" onclick="selectNone(this)"><span class="glyphicon glyphicon-remove-circle"></span></a>
                        <h3 class="panel-title" style="color:@string.Format("#{0:X2}{1:X2}{2:X2}", fntColor.R, fntColor.G, fntColor.B);">@m.AppName - @m.ModuleName</h3>
                    </div>
                    <div class="panel-body">
                        @foreach (var a in m.Actions)
                        {
                            <div class="input-group input-group-sm" data-actionID="@a.ActionID" data-actionKind="@a.Kind" style="width: 219px; float: left;">
                                <div class="input-group-addon"><input type="checkbox" checked="@a.Checked" /></div>
                                @switch (a.Kind)
                                {
                                    case 0: <span class="form-control" style="background-color:lightblue">@a.ActionName</span><div class="input-group-addon icon"><span class="glyphicon glyphicon-comment" title="系统组件"></span></div> break;
                                    case 1: <span class="form-control" style="background-color:khaki">@a.ActionName</span><div class="input-group-addon icon"><span class="glyphicon glyphicon-list-alt" title="功能视图"></span></div> break;
                                    case 2: <span class="form-control" style="background-color:cadetblue">@a.ActionName</span><div class="input-group-addon icon"><span class="glyphicon glyphicon-menu" title="系统菜单"></span></div> break;
                                    case 3: <span class="form-control" style="background-color:palegreen">@a.ActionName</span><div class="input-group-addon icon"><span class="glyphicon glyphicon-transfer" title="Ajax功能服务"></span></div> break;
                                    case 4: <span class="form-control" style="background-color:pink">@a.ActionName</span><div class="input-group-addon icon"><span class="glyphicon glyphicon-globe" title="系统功能服务"></span></div> break;
                                    case 5: <span class="form-control" style="background-color:blanchedalmond">@a.ActionName</span><div class="input-group-addon icon"><span class="glyphicon glyphicon-user" title="管理员功能"></span></div> break;
                                    case 6: <span class="form-control" style="background-color:antiquewhite">@a.ActionName</span><div class="input-group-addon icon"><span class="glyphicon glyphicon-home" title="平台管理功能"></span></div>break;
                                }
                            </div>
                        }
                    </div>
                </div>
            }
        </div>
    </div>
</div>
<div class="form-group">
    <input value="保存" class="btn btn-primary" onclick="Save()" />
</div>
<script type="text/javascript">
    function GetTreeSelectedVal(Nodes, data) {
        var result = false;
        for (var i = 0; i < Nodes.length; i++) {
            if (GetTreeSelectedVal(Nodes[i].children, data) || Nodes[i].checked) {
                var j = { actionID: Nodes[i].ID, kind: @((int)Von.Web.EControlKind.CK_Menu) };
                data.push(j);
                result = true;
            }
        }
        return result;
    }
    function Save() {
        var items = $("#editor [data-actionID]");
        var data = [];
        for (var i = 0; i < items.length; i++) {
            if ($(items[i]).find("input").prop("checked")) { var j = { actionID: $(items[i]).attr("data-actionID"), kind: $(items[i]).attr("data-actionKind") }; data.push(j); }
        }
        var Node = $('#MenuTree').tree('getRoots');
        GetTreeSelectedVal(Node, data);
        AjaxPostData('@Url.Ajax("SaveRight")', { Rolename: $("#RoleName").val(), Rights: data }, function (result) { window.location.replace("/Admin/RoleManage"); });
    }
    function selectAll(a) {
        $(a).parent().next().find("input").prop("checked", true);

    }
    function selectNone(a) {
        $(a).parent().next().find("input").prop("checked", false);
    }
</script>

@Html.Partial("~/Views/Shared/_TreeScriptsPartial.cshtml")

@Html.Import(Von.Web.Html.EImportSection.BodyTrailler, "MenuRight",
    @<script>
        $('#MenuTree').tree({
            data: @Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(Model.Menus)),
            checkbox: true,
            cascadeCheck: false,
            onCheck: function (node) {
                var pnode = $('#MenuTree').tree('getParent', node.target);
                if (pnode != null && node.checked) $('#MenuTree').tree('check', pnode.target);
                var cnode = $('#MenuTree').tree('getChildren', node.target);
                for (var i = 0; i < cnode.length; i++) {
                    $('#MenuTree').tree(node.checked ? 'check' : 'uncheck', cnode[i].target);
                }
            }
        });
    </script>
)